﻿//Added by: Moses
//Added on: 2012/01/20

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Search
    {
        

        private List<Packages> _packages = new List<Packages>(new DbAccess().SelectPackages());
        private List<Products> _products = new List<Products>(new DbAccess().SelectProducts());
        private List<Suppliers> _suppliers = new List<Suppliers>(new DbAccess().SelectSuppliers());
        private List<ProductsSuppliers> _prodSupList = new List<ProductsSuppliers>(new DbAccess().SelectProductsSuppliers());
        private List<PackagesProductsSupplier> _pkgProdList = new List<PackagesProductsSupplier>(new DbAccess().SelectPackagesProSupp());

        private int _supId;
        private int _proId;
        private bool _supAll = false;
        private bool _proAll = false;

        public List<ProductsSuppliers> filterProSup(Form form)
        {
            form.

            //get the values in the search filter comboboxes

            //get the control
            Control[] supCtrl = form.Controls.Find("cmbSearchSuppliers", true);
            ComboBox supTemp = (ComboBox)supCtrl[0];

            //check for a selection first
            if ((supTemp.SelectedIndex != -1) && ((supTemp.SelectedItem).GetType() == typeof(Suppliers)))
                { _supId = ((Suppliers)supTemp.SelectedItem).SupplierId; }
            else { _supAll = true; }

            //repeat for products
            Control[] proCtrl = form.Controls.Find("cmbSearchProducts", true);
            ComboBox proTemp = (ComboBox)proCtrl[0];

            if ((proTemp.SelectedIndex != -1) && ((proTemp.SelectedItem).GetType() == typeof(Products)))
                { _proId = ((Products)proTemp.SelectedItem).ProductId; }
            else { _proAll = true; }


            if (!_supAll && !_proAll)
            {
                //return productsupplier list filtered on productid and supplierid
                List<ProductsSuppliers> tmpRes = new List<ProductsSuppliers>();
                List<ProductsSuppliers> results = new List<ProductsSuppliers>();

                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.ProductId == _proId) { tmpRes.Add(entry); } }

                foreach (ProductsSuppliers entry in tmpRes)
                    { if (entry.SupplierId == _supId) { results.Add(entry); } }

                //now check the selected condition
                
                


                return results;

            }

            else if (_supAll && !_proAll)
            {
                //return productssupplier list filtered on productid
                
                List<ProductsSuppliers> results = new List<ProductsSuppliers>();
                
                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.ProductId == _proId) { results.Add(entry); } }

                return results;

            }

            else if (!_supAll && _proAll)
            {
                //return productsupplier list filtered on supplierid

                List<ProductsSuppliers> results = new List<ProductsSuppliers>();

                foreach (ProductsSuppliers entry in _prodSupList)
                    { if (entry.SupplierId == _supId) { results.Add(entry); } }

                return results;

            }

            
            else { return _prodSupList; } //none or invalid or inconceivable selection made for filtering :D

        }


    }
}
